package com.dmgis.qxfw.utils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.github.kevinsawicki.http.HttpRequest;

public class HttpRequestUtil {
	public String sendGet(String url,Map<String ,Object> param){
		String result = "";
		HttpRequest httpRequest = HttpRequest.get(url).trustAllCerts().trustAllHosts().
				form(param).connectTimeout(60000).readTimeout(60000);
		result = httpRequest.body();
		return result;
	}

	public String sendPost(String url,Map<String ,Object> param){
		String result = "";
		HttpRequest httpRequest = HttpRequest.post(url).trustAllCerts().trustAllHosts().
				form(param).connectTimeout(60000).readTimeout(60000);
		result = httpRequest.body();
		return result;
	}

	public String sendGet1(String geturl){
		try {
			URL url = new URL(geturl);
			//得到connection对象。
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			//设置请求方式
			connection.setRequestMethod("GET");
			//连接
			connection.connect();
			//得到响应码
			int responseCode = connection.getResponseCode();
			if(responseCode == HttpURLConnection.HTTP_OK){
				//得到响应流
				BufferedReader reader = new BufferedReader(new InputStreamReader
						(connection.getInputStream(), StandardCharsets.UTF_8));
				StringBuilder sb = new StringBuilder();
				String l;
				while ((l = reader.readLine()) != null) {
					sb.append(l).append("\n");
				}
				reader.close();
				// 断开连接
				connection.disconnect();
				return sb.toString();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String senfGet2(String  fullurl)
	{
		String result="";
		try {
			URL url = new URL(fullurl);
			InputStream is = null;
			URLConnection con=url.openConnection();
			con.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
			con.setConnectTimeout(120000);
			con.setReadTimeout(120000);
			con.connect();
			try {
				is = con.getInputStream();
				BufferedReader reader = null;
				try {
					reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
					String s="";
					String linesep = System.getProperty("line.separator");
					while((s = reader.readLine())!=null){
						result += s+linesep ;
					}
					reader.close();
				} catch (Exception e) {
					e.printStackTrace();
				}finally {
					if (reader != null) {
						try {
							reader.close();
						} catch (Exception e1) {
						}
					}
				}
				is.close();
			}catch (FileNotFoundException e2) {
				;
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return result;
	}

	public String sendPost1(String ADD_URL, JSONObject param){
		try {
			// 创建连接
			URL url = new URL(ADD_URL);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);//允许写出
			connection.setDoInput(true);//允许读入
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);//不使用缓存
			connection.setInstanceFollowRedirects(true);
			//设置请求头
			connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");//设置参数类型是json格式

			connection.connect();

			// POST请求,包装成json数据
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
			writer.write(param.toJSONString());
			writer.close();

			// 读取响应
			int responseCode = connection.getResponseCode();
			if(responseCode == HttpURLConnection.HTTP_OK){
				//得到响应流
				BufferedReader reader = new BufferedReader(new InputStreamReader
						(connection.getInputStream(), StandardCharsets.UTF_8));
				StringBuilder sb = new StringBuilder();
				String l;
				while ((l = reader.readLine()) != null) {
					sb.append(l).append("\n");
				}
				reader.close();
				// 断开连接
				connection.disconnect();
				return sb.toString();
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
